/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "ConnectFrame.h"

#include <QtGui/QGridLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QComboBox>

const int	c_postgreDefaultPort = 5432;
const int	c_mySqlDefaultPort = 3306;

/**
**/
ConnectFrame::ConnectFrame( QWidget *parent, const QString &host, int port, const QString &user,
		const QString &pass, const QString &dbName, Bio::SqlServer::ServerType type ) : QFrame( parent )
{
	buildUI();

	if( port < 0 ){
		if( type == Bio::SqlServer::PostgreSQL )
			port = c_postgreDefaultPort;
		else if( type == Bio::SqlServer::MySQL )
			port = c_mySqlDefaultPort;
		else
			port = 0;
	}

	m_serverTypeCombo->setCurrentIndex( type );
	m_hostEdit->setText( host );
	m_portEdit->setText( QString::number( port ) );
	m_userEdit->setText( user );
	m_passEdit->setText( pass );
	m_dbNameEdit->setText( dbName );
}

/**
**/
ConnectFrame::~ConnectFrame()
{
}

/**
**/
Bio::SqlServer::ServerType ConnectFrame::getServerType() const
{
	int	idx = m_serverTypeCombo->currentIndex();

	if( idx == 0 )
		return Bio::SqlServer::PostgreSQL;
	else if( idx == 1 )
		return Bio::SqlServer::MySQL;

	return Bio::SqlServer::PostgreSQL;
}

/**
**/
QString ConnectFrame::getHost() const
{
	return m_hostEdit->text();
}

/**
**/
int ConnectFrame::getPort() const
{
	return m_portEdit->text().toInt();
}

/**
**/
QString ConnectFrame::getUser() const
{
	return m_userEdit->text();
}

/**
**/
QString ConnectFrame::getPassword() const
{
	return m_passEdit->text();
}

/**
**/
QString ConnectFrame::getDBName() const
{
	return m_dbNameEdit->text();
}

/**
**/
void ConnectFrame::updateServerType( int index )
{
	if( index == 0 )
		m_portEdit->setText( QString::number( c_postgreDefaultPort ) );
	else if( index == 1 )
		m_portEdit->setText( QString::number( c_mySqlDefaultPort ) );
}

/**
**/
void ConnectFrame::buildUI()
{
	int			row = 0;
	int			col = 0;
	QLabel		*lbl;
	QGridLayout	*layout = new QGridLayout;

	setLayout( layout );

	lbl = new QLabel( tr( "Database Type:" ), this );
	layout->addWidget( lbl, row, col++ );

	m_serverTypeCombo = new QComboBox( this );
	layout->addWidget( m_serverTypeCombo, row, col++, 1, 3 );
	m_serverTypeCombo->addItem( "PostgreSQL" );
	m_serverTypeCombo->addItem( "MySQL" );
	connect( m_serverTypeCombo, SIGNAL(activated(int)), this, SLOT(updateServerType(int)) );

	row++;
	col = 0;

	lbl = new QLabel( tr( "Host:" ), this );
	layout->addWidget( lbl, row, col++ );

	m_hostEdit = new QLineEdit( this );
	layout->addWidget( m_hostEdit, row, col++ );

	lbl = new QLabel( tr( "Port:" ), this );
	layout->addWidget( lbl, row, col++ );

	m_portEdit = new QLineEdit( this );
	layout->addWidget( m_portEdit, row, col++ );

	row++;
	col= 0;

	lbl = new QLabel( tr( "User:" ), this );
	layout->addWidget( lbl, row, col++ );

	m_userEdit = new QLineEdit( this );
	layout->addWidget( m_userEdit, row, col++, 1, 3 );

	row++;
	col = 0;

	lbl = new QLabel( tr( "Password:" ), this );
	layout->addWidget( lbl, row, col++ );

	m_passEdit = new QLineEdit( this );
	m_passEdit->setEchoMode( QLineEdit::Password );
	layout->addWidget( m_passEdit, row, col++, 1, 3 );

	row++;
	col = 0;

	lbl = new QLabel( tr( "Database Name:" ), this );
	layout->addWidget( lbl, row, col++ );

	m_dbNameEdit = new QLineEdit( this );
	layout->addWidget( m_dbNameEdit, row, col++, 1, 3 );

	row++;
	col = 0;

	layout->setColumnStretch( 1, 100 );
	layout->setRowStretch( row, 100 );

	setOnlineHelp();
}

/**
**/
void ConnectFrame::setOnlineHelp()
{
	m_serverTypeCombo->setToolTip( "Select the type of server you want to connect to." );
	m_serverTypeCombo->setWhatsThis( "Select the type of server you want to connect to. "
		"Snapshot currently supports PostgreSQL and MySQL servers." );

	m_hostEdit->setToolTip( "The IP address or host name of the server." );
	m_hostEdit->setWhatsThis( "Enter the host name or IP address of the server. "
		"If the server is running on the same computer as Snapshot, use 127.0.0.1 "
		"as the host name. If the server is on another computer, typically you will "
		"enter it's IP address. Check with your database administrator if you are unsure "
		"what the value should be." );

	m_portEdit->setToolTip( "The port that the server is listening on." );
	m_portEdit->setWhatsThis( "Enter the port that the server is listening on. "
		"For MySQL, the default is 3306. For PostgreSQL, the default is 5432. "
		"Check with your database administrator if you are unsure what the value should be." );

	m_userEdit->setToolTip( "Enter the user name to use to connect to the database." );
	m_userEdit->setWhatsThis( "Enter the user name to use to connect to the database. "
		"This is the user name that will be used to log on to the database server. At a "
		"minimum, the user must have read access for the BioSQL database." );

	m_passEdit->setToolTip( "Enter the password for the user account." );
	m_passEdit->setWhatsThis( "Enter the password for the user account. This is the "
		"password for the user account that will be used to log onto the database server." );

	m_dbNameEdit->setToolTip( "Enter the name of the BioSQL database." );
	m_dbNameEdit->setWhatsThis( "Enter the name of the BioSQL database. This is the name of the "
		"BioSQL database on the database server." );
}
